package com.apes.scm.masterdata.stock.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.scm.masterdata.stock.model.StockUsage;
import com.apes.scm.masterdata.stock.model.UsageBusiness;
import com.apes.scm.masterdata.stock.repository.UsageBusinessRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能：库区属性业务单据配置
 *
 * @author HuJianlin
 * @create 2018-03-14 14:35
 */
@Service("usageBusinessService")
public class UsageBusinessService extends DomainService {
    @Autowired
    public UsageBusinessRepository usageBusinessRepository;

    public UsageBusiness save(SimpleRequest request) {
        UsageBusiness usageBusiness = request.getO(UsageBusiness.class);
        return usageBusinessRepository.saveAndFlush(usageBusiness);
    }

    /**
     * 功能：查询
     * @param request
     */
    public List<UsageBusiness> findAll(SimpleRequest request) {
        return usageBusinessRepository.findAll();
    }


    /**
     * 功能：查询
     * @param request
     */
    @Listener(topic = "event:cm.stockUsage.findByBusinessTypeAndValid")
    public List<String> findByBusinessTypeAndValid(SimpleRequest request) {
        String businessType = request.get("businessType");
        List<UsageBusiness> usageBusinessList = usageBusinessRepository.findByBusinessTypeAndValid(businessType,true);
        List<String> stockUsageList = usageBusinessList.stream().map(usageBusiness-> usageBusiness.getUsage().getId()).distinct().collect(Collectors.toList());
        return stockUsageList;
    }
}
